/*
 * @lc app=leetcode.cn id=1260 lang=typescript
 *
 * [1260] 二维网格迁移
 */

// @lc code=start
// 将二维数组退化为数组操作即可
function shiftGrid(grid: number[][], k: number): number[][] {
    let newGrid: number[] = [];
    grid.forEach((i) => {
        newGrid = [...newGrid, ...i];
    });
    let length = newGrid.length;
    const heads = newGrid.slice(newGrid.length - (k % length), newGrid.length);
    newGrid.unshift(...heads);
    newGrid.length = length;

    return newGrid.reduce(
        (col, cur, index) => {
            const last = col[col.length - 1];
            if (last.length === grid[0].length) {
                col.push([cur]);
            } else {
                last.push(cur);
            }
            return col;
        },
        [[]] as number[][]
    );
}
// @lc code=end
